﻿//
// Copyright (c) 2010
// by Ashish Agrawal and Chris Paterra
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Web.Mvp;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Entities.Modules;
using WebFormsMvp;
using System.Web.UI.WebControls;
using SimpleBlog.Components.Models;
using SimpleBlog.Components.Common;
using SimpleBlog.Components.Presenters;
using SimpleBlog.Components.Entities;
using DotNetNuke.Services.Localization;
using DotNetNuke.Common;
using DotNetNuke.Security;

namespace SimpleBlog.Components.Views
{
	[PresenterBinding(typeof(EntriesPresenter))]
	public partial class Entries : ModuleView<EntriesModel>, IEntriesView
	{

		#region Constructor

		public Entries()
		{
			AutoDataBind = false;
		}

		#endregion
		
		public event EventHandler<UIListEventArgs<HyperLink, Image, HyperLink, Literal, EntryInfo, HyperLink, Literal>> ItemDataBound;

		/// <summary>
		/// Handles formatting of individual items
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void RptEntriesItemDataBound(object sender, RepeaterItemEventArgs e) {
			var link = (HyperLink)e.Item.FindControl("hlTitle");
			var icon = (Image)e.Item.FindControl("imgEdit");
			var editLink = (HyperLink) e.Item.FindControl("hlEdit");
			var summary = (Literal)e.Item.FindControl("litSummary");
			var blogEntry = (EntryInfo)e.Item.DataItem;
			var hlComments = (HyperLink)e.Item.FindControl("hlComments");
			var litDate = (Literal)e.Item.FindControl("litDate");

			ItemDataBound(this, new UIListEventArgs<HyperLink, Image, HyperLink, Literal, EntryInfo, HyperLink, Literal>(link, icon, editLink, summary, blogEntry, hlComments, litDate));
		}

		/// <summary>
		/// 
		/// </summary>
		public void Refresh()
		{
			try
			{
				rptEntries.DataSource = Model.PublishedEntries;
				rptEntries.DataBind();
			}
			catch (Exception exc)
			{
				DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
				throw;
			}

		}

	}
}